<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$this->model('model')->checkauth($merchid);
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
$tabs=array(
	array('op'=>'edit','title'=>'添加项目_ADD Goods'),
	
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
$shopset=plugin('jy_weishop')->plugin_setting('shopset');
//上架项目
$myshop=pdo_get('jy_merch_user',array('id'=>$merchid));	
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid,'onepluginname'=>'jy_mfuwu','type'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	$tip='【温馨提示】：服务的具体保修项目，比如：技师美容，上门保洁，空调维修，抽油烟机报修，地板报修等！';
	$install = check_permission('jy_poster');
	if(!$install){
		$tip .='</br><red>【注意】：还未安装《活动海报》！项目详情不显示生成海报按钮！</red>';
	}
	$pc_set=plugin('jy_weishop')->plugin_setting('pc_set');
	$substrlength=13;
	if($pc_set['is_en']==1){
		$tip='';
		$substrlength=20;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','price','status','createtime','total','salenum','unit','saletype','nochecked');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,$substrlength);
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['url2']='jy_mfuwu/app.goods.detail//id='.$item['id'];
				$item['url']=custom_url($item['url2'],true);
				$item['salenum2']=pdo_getsum('jy_weishop_order_goods',array('goodsid'=>$item['id']),'total')?:0;
				$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
				$item['merchname']=$merch['merchname']?:'';
				$item['merchlogo']=$merch['logo'];
				$item['salecate']=$merch['salecate']?:'';
				if($pc_set['is_en']==1){
					$shopset=$shopset;
					$item['merchname']=$merch['merchname']?:$shopset['title'];
					$item['salecate']=$merch['salecate']?:'NO';
					$item['salenum2']=$item['salenum']?:$item['salenum2'];
					$item['pcatetitle']=$item['pcatetitle']?:$shopset['title'];
					$item['ccatetitle']=$item['ccatetitle']?:'NO';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/A';
					$item['oneplugintitle']='oneplugintitle';
				}else{
					$item['oneplugintitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'电商客';
					$item['merchname']=$merch['merchname']?:'平台自营';
					$item['salecate']=$merch['salecate']?:'无';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/次';
					$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
					$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				}
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序_The sorting','type'=>'string'),
			array('type'=>'img_string','title'=>'项目名称_Goods title','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'项目分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量_Inventory/Sales',
			'data'=>array(
				array('title'=>'库存：_Inventory：','name'=>'total'),
				array('title'=>'销量：_Sales：','name'=>'salenum2','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'价格(点击编辑↓)_Price','type'=>'string','set'=>'set'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'nochecked','title'=>'审核状态','type'=>'radio','options'=>'nochecked'),
			array('name'=>'saletype','title'=>'项目属性_attribute','type'=>'checkbox','options'=>'saletypes'),
			array('name'=>'url','title'=>'复制链接_Copy Link','type'=>'copy'),
			array('name'=>'url2','title'=>'系统短链','type'=>'copy'),
		),'action'=>array(
			array('type'=>'href','action'=>'activity.buyagain.list','title'=>'设置活动_Set activity'),
			array('type'=>'post','op'=>'copy','title'=>'复制项目_Copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_mfuwu/manage.goods_list/setstatus0/','title'=>'一键下架_Shelves'),
			array('type'=>'post','url'=>'jy_mfuwu/manage.goods_list/setstatus2/','title'=>'一键售罄_Sold out'),
			array('type'=>'post','url'=>'jy_mfuwu/manage.goods_list/setstatus3/','title'=>'一键加入回收站_Recycle'),
			array('type'=>'post','url'=>'jy_mfuwu/manage.goods_list/upgoods/','title'=>'一键更新项目权限_Upgoods'),
			array('type'=>'post','url'=>'jy_mfuwu/manage.goods_list/upseven/','title'=>'一键支持退款_Uptask'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'项目名称_Goods'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'项目分类_Groups','options'=>'groups'),
			array('name'=>'saletype','width'=>15,'type'=>'radio','title'=>'属性_Saletype','options'=>'saletypes'),
		)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'is_fuwu'=>1,'mid'=>0,'isshow'=>1,'parentid'=>0);
	$groupspram['onepluginname']='jy_mfuwu';
	$return['params']['onepluginname']='jy_mfuwu';
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['nochecked']=array('审核通过','未审核');
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/goods_list');
	}else{
		$return['htmls']=$htmls;
	}


	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=round($post['price'],2);
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$isread=pdo_count('jy_weishop_order_goods',array('goodsid'=>$id),'id');
		$item=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['pluginname']=='jy_taobaoke' && $item['pluginname']=='jy_jdk' && $item['pluginname']=='jy_pdd'){
			exi('淘宝客，京东客，拼多多客等项目不允许编辑','error','list');
		}
		$item['dispatch']=pdo_get('jy_weishop_goods_dispatch',array('goodsid'=>$id));
		if($item['istime']==1){
			$item['cuxiao']=2;
		}elseif($item['isdiscount']==1){
			$item['cuxiao']=1;
		}
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$item['oldspecs']=$item['specs'];
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight','ya_price','zu_data'),'','paixu asc');
		if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('id','specid','title'));
					if($op['specid']){
						$i[$op['specid']]=$op;
					}
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$item['specs']['tds']=$option_list;
		}
	}else{
		$item=array('status'=>1,'total'=>188,'totalcnf'=>2);
	}
	$item['verifygoodslimitdate']=$item['verifygoodslimitdate']?date('Y-m-d H:i',$item['verifygoodslimitdate']):date('Y-m-d H:i',time());
	if(!$item['dispatch']){
		$item['dispatch']=array('fullmoney'=>'');
	}
	$shopset=$shopset;
	$return['offcodeunit']=$shopset['offcodeunit'];
	foreach($return['offcodeunit'] as $k=>&$offv){
		$offv['id']=$k;
	}
	if(intval($shopset['goodscodenum'])>0){
		$goodsnum=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),'id');
		$shopset['goodscodenum']=intval($shopset['goodscodenum']);
		$item['offcode']=$selfnumber=sprintf("%0".$shopset['goodscodenum']."d", $goodsnum);
	}
	if($item['showlevels']){
		$fastvalue=substr($item['showlevels'],0,1);
		if($fastvalue!=';'){
			$item['showlevels']=';'.$item['showlevels'];
		}
		$lastvalue=substr($item['showlevels'],-1,1);
		if($lastvalue!=';'){
			$item['showlevels']=$item['showlevels'].';';
		}
	}else{
		$item['showlevels']='';
	}
	if($item['buylevels']){
		$fastvalue=substr($item['buylevels'],0,1);
		if($fastvalue!=';'){
			$item['buylevels']=';'.$item['buylevels'];
		}
		$lastvalue=substr($item['buylevels'],-1,1);
		if($lastvalue!=';'){
			$item['buylevels']=$item['buylevels'].';';
		}
	}else{
		$item['buylevels']='';
	}
	if($item['showgroups']){
		$fastvalue=substr($item['showgroups'],0,1);
		if($fastvalue!=';'){
			$item['showgroups']=';'.$item['showgroups'];
		}
		$lastvalue=substr($item['showgroups'],-1,1);
		if($lastvalue!=';'){
			$item['showgroups']=$item['showgroups'].';';
		}
	}else{
		$item['showgroups']='';
	}
	if($item['buygroups']){
		$fastvalue=substr($item['buygroups'],0,1);
		if($fastvalue!=';'){
			$item['buygroups']=';'.$item['buygroups'];
		}
		$lastvalue=substr($item['buygroups'],-1,1);
		if($lastvalue!=';'){
			$item['buygroups']=$item['buygroups'].';';
		}
	}else{
		$item['buygroups']='';
	}
	if($_SESSION['current_plugin']=='jy_pingdd'){
		$item['is_group']=1;
	}
	if($item['api_pluginname']=='jy_supplier'){
		$item['supplier_id']=$item['api_id'];
	}
	$return['numtypes']=array('0'=>'天','1'=>'月','2'=>'年',);
	$return['item']=$item;
	$return['item']['storeids']=json_decode($return['item']['storeids'],true);
	$return['item']['param']=json_decode($return['item']['param'],true);
	$return['item']['param0']=$return['item']['param'][0];
	$return['item']['param1']=$return['item']['param'][1];
	$return['item']['param2']=$return['item']['param'][2];
	$return['item']['param3']=$return['item']['param'][3];
	$return['item']['param1title']=$return['item']['param']['param1title'];
	$return['item']['param2title']=$return['item']['param']['param2title'];
	$return['item']['param3title']=$return['item']['param']['param3title'];
	$return['item']['images']=json_decode($return['item']['images'],true);
	$return['item']['diyfields']=$return['item']['diyfields']?json_decode($return['item']['diyfields'],true):array();
	$return['item']['pcd']=json_decode($return['item']['pcd'],true);
	$return['item']['diyprice']=json_decode($return['item']['diyprice'],true);
	$return['item']['deductset']=json_decode($return['item']['deductset'],true);
	$return['item']['commissions']=json_decode($return['item']['commissions'],true);
	$catelevel=2;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'title','title'=>'项目名称','type'=>'string'),
					array('name'=>'image','title'=>'项目图片','type'=>'image','help'=>'建议尺寸：宽580px*高650rpx'),
					array('name'=>'video','title'=>'项目视频','type'=>'video','help'=>'默认优选显示视频，如果想关闭，请移步基础设置-其他设置'),
					array('name'=>'subtitle','title'=>'副标题','type'=>'text','help'=>'副标题的长度请控制在100字以内'),
					array('name'=>'shorttitle','title'=>'短标题','type'=>'string','help'=>'项目短标题 用于快递打印,以及小型热敏打印机打印'),
				    array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类','help'=>'[温馨提示]：如果场景下，开启了独立项目，此处只显示本场景下的分类',
						'name2'=>'ccate','options2'=>'ccates','title2'=>'请选择二级分类',
						'name3'=>'scate','options3'=>'scates','title3'=>'请选择三级分类',
					),
					array('name'=>'saletype','title'=>'项目属性','type'=>'checkbox','options'=>'saletype'),
					array('name'=>'price','title'=>'项目价格','type'=>'string','help'=>'<red>项目价格</red>解释：如果门店设置了项目价格，将以门店价格为准！'),
					array('name'=>'originalprice','title'=>'原价','type'=>'string'),
					array('name'=>'marketprice','title'=>'市场价','type'=>'string','help'=>'请填写真实市场价/同行价，方便后台统计节省数据'),
					array('name'=>'costprice','title'=>'成本价','type'=>'string'),
					array('name'=>'images','title'=>'项目图片','type'=>'images','help'=>'建议尺寸：宽580px*高650rpx'),
					array('name'=>'unit','title'=>'项目单位','type'=>'string'),
					//array('name'=>'seven','title'=>'退换货','type'=>'switch','help'=>'支持单项目退换货后，需移步设置-退款设置-设置好退款信息！才能生效！'),
					array('name'=>'status','title'=>'上架','type'=>'switch'),
					array('name'=>'no_onebuy','title'=>'不可单独购买？','type'=>'switch','help'=>'必须跟《可单独购买》的项目一起下单才行！'),
				)
			),
			/*array(
				'title'=>'自提/核销',
				//'show'=>array('type'=>array(0,3)),
				'list'=>array(
					array('name'=>'isverify','title'=>'支持核销','type'=>'switch','show'=>array('type'=>array(0,1,2)),'help'=>'计次项目，默认开启核销'),
					array('name'=>'storeids','title'=>'选择线下门店','type'=>'choose_many','action'=>'jy_weishop/store.get_store/many/','choose_title'=>'选择门店','help'=>'门店信息--》设置门店，留空则所有门店都可以核销！开启核销或者设置-->项目详情设置-->开启门店显示，项目详情页才会显示门店，默认显示最近的一个！'),
					array('name'=>'verifytype','title'=>'核销方式','type'=>'radio','options'=>'verifytype','show'=>array('type'=>array(0,1,2)),'help'=>'按订单核销： 不管购买多少 一次核销完成按次核销： 一个消费码使用多次（购买的数量）按消费码核销： 多个消费码 一次核销一个'),
					array('name'=>'usetime','title'=>'自动使用期限','type'=>'string','show'=>array('type'=>array(0,1,2)),'help'=>'项目购买X天后自动使用，自动使用后无法退款，设置为0则没有限制'),
					array('name'=>'verifygoodsnum','title'=>'核销次数','type'=>'unitstring','unit'=>'次','show'=>array('type'=>array(3)),'help'=>'此项目可以核销次数,不填或填写0及以下为默认不限次数'),
					array('name'=>'verifygoodslimittype','title'=>'有效期类型','type'=>'radio','options'=>'verifygoodslimittype','show'=>array('type'=>array(3)),'help'=>'自购买之日起多少天内有效,不写默认365天'),
					array('name'=>'verifygoodsdays','title'=>'有效天数','type'=>'unitstring','unit'=>'天','show'=>array('type'=>array(3),'verifygoodslimittype'=>0),'help'=>'自购买之日起多少天内有效,不写默认365天'),
					array('name'=>'verifygoodslimitdate','title'=>'过期日期','type'=>'time','unit'=>'天','show'=>array('type'=>array(3),'verifygoodslimittype'=>1),'help'=>'无论何时购买此项目,到达指定时间后都将过期,无法核销.'),
				)
			),*/
			array(
				'title'=>'套餐规格',
				'list'=>array(
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：【规格添加教程】：</br>1、点<red>添加规格</red>，再点<red>+添加规格项</red>；2、添加好之后再点《<red>刷新规格项目表</red>》；3、然后设置不同规格的价格，库存，图片等！'),
					array('name'=>'specs','title'=>'规格','type'=>'specs_new'),
				)
			),
			array(
				'title'=>'详情',
				'list'=>array(
					array('name'=>'salenum','title'=>'已服务数','type'=>'string'),
					array('name'=>'isshowsales','title'=>'不显示销量？','type'=>'switch'),
					//array('name'=>'pcd','title'=>'所在地','type'=>'pcd','help'=>'项目所在地，显示在项目详情页，如果不选择，则显示商城所在地'),
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：默认图片宽度是100%；图片之间不回车换行的话，追加在后面的话，图片之间就没有空白线！！'),
					array('name'=>'content','title'=>'详情','type'=>'richtext','help'=>''),
					array('name'=>'buyshow','title'=>'购买后可见','type'=>'switch','help'=>'开启后购买过的项目才会显示下面内容！！'),
					array('name'=>'buycontent','title'=>'购买后显示内容','type'=>'richtext','help'=>''),
				)
			),
			array(
				'title'=>'收费标准',
				'list'=>array(
						array('name'=>'param0','title'=>'收费说明','type'=>'text'),
						array('name'=>'param1title','title'=>'[服务收费]名称自定义','type'=>'string'),
						array('type'=>'array','title'=>'服务收费','name'=>'param1','add_tip'=>'添加一个收费',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),
										
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),						
									)
						),
						array('name'=>'param2title','title'=>'[材料收费]名称自定义','type'=>'string'),
						array('type'=>'array','title'=>'材料收费','name'=>'param2','add_tip'=>'添加一个收费',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),
										
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),						
									)
						),
						array('name'=>'param3title','title'=>'[其他服务收费]名称自定义','type'=>'string'),
						array('type'=>'array','title'=>'其他服务收费','name'=>'param3','add_tip'=>'添加一个收费',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),
										
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'名称'),
										array('type'=>'string','name'=>'value','title'=>'费用'),						
									)
						),
				)
			),			
			array(
				'title'=>'定价/购买/权限',
				'list'=>array(
					array('name'=>'no_de_sales','title'=>'不参与的优惠活动','type'=>'checkbox','options'=>'de_sales'),
					array('name'=>'maxbuy','title'=>'单次最多购买','type'=>'string','help'=>''),
					array('name'=>'minbuynum','title'=>'单次最低购买','type'=>'string','help'=>''),
					array('name'=>'usermaxbuy','title'=>'最多购买','type'=>'string','help'=>''),
					array('name'=>'showlevels','title'=>'会员等级浏览权限','type'=>'checkbox','options'=>'levels','help'=>''),
					array('name'=>'buylevels','title'=>'会员等级购买权限','type'=>'checkbox','options'=>'levels','help'=>''),
					array('name'=>'showgroups','title'=>'会员组浏览权限','type'=>'checkbox','options'=>'groups','help'=>''),
					array('name'=>'buygroups','title'=>'会员组购买权限','type'=>'checkbox','options'=>'groups','help'=>''),
					array('name'=>'isdiyprice','title'=>'启用个性定价','type'=>'switch','help'=>'<red>个性定价</red>优选于会员折扣，如果所属会员设置了个性定价，会员折扣将失效。<red>带规格的项目，个性定价将无效</red>'),
					array('type'=>'array','title'=>'个性定价','name'=>'diyprice','add_tip'=>'添加一组个性定价',
						'list'=>array(
									array('type'=>'string','name'=>'price','title'=>'个性定价'),
									array('type'=>'string','name'=>'pricerate','title'=>'多规格定价%'),
									array('type'=>'select','name'=>'levelid','title'=>'所属会员等级','options'=>'levels2'),			
								),
								'details'=>array(
									array('type'=>'unitstring','unit'=>'元','name'=>'price','title'=>'个性定价','help'=>'必须是数字'),	
									array('type'=>'unitstring','unit'=>'%','name'=>'pricerate','title'=>'多规格定价%','help'=>'必须是数字，原规格的价格%'),
									array('type'=>'select','name'=>'levelid','title'=>'所属会员等级','options'=>'levels2','help'=>'无会员等级，请先移步会员-会员等级，添加！'),
								)
					),
				)
			),
			array(
				'title'=>'营销',
				'list'=>array(
					array('title'=>'更多营销设置，请移步-->活动营销,添加需要参与活动的项目！','type'=>'tip'),
					array('type'=>'title','title'=>'抵扣设置'),
					array('name'=>'isdiscount','title'=>'开启独立抵扣？','type'=>'switch','help'=>'默认按平台设置的抵扣！'),
					array('type'=>'switch','name'=>'deductset.deduct_credit1','title'=>'积分抵扣'),
					array('type'=>'unitstring','unit'=>'比1','name'=>'deductset.deduct_bili','title'=>'积分抵扣比例','help'=>'例如设置为10,即10积分抵1元,项目最多抵扣的数目需要在项目【营销设置】中单独设置'),
					array('type'=>'unitstring','unit'=>'%','name'=>'deductset.deduct_maxfeebili','title'=>'积分最大抵扣的额度比例','help'=>'必须是数字，默认50；积分最大抵扣的费用比例,例如50，表示最大可抵扣一半'),
					array('type'=>'switch','name'=>'deductset.deduct_credit2','title'=>'余额抵扣','help'=>'余额属于等比例抵扣'),
					array('type'=>'unitstring','unit'=>'元','name'=>'deductset.deduct_credit2max','title'=>'余额最大抵扣','help'=>'如果设置0留空，则支持全额抵扣'),
					array('type'=>'switch','name'=>'deductset.deduct_dispatch','title'=>'是否可以抵扣运费'),
					array('type'=>'title','title'=>'满返设置'),
					array('title'=>'积分赠送设置，请移步-->活动营销-->购物送积分！','type'=>'tip'),
					array('type'=>'string','name'=>'fang_credit2','title'=>'余额返现','help'=>'会员购物返现, 如果不填写或填写0，则默认为不返现，如果带%则为按成交价格的比例计算返现</br> 如: 购买2件，设置10 元, 不管成交价格是多少， 则购买后获得20元返现</br>如: 购买2件，设置10%元, 成交价格2 * 100= 200， 则购买后获得 20 元返现（200*10%）'),
					array('type'=>'title','title'=>'虚拟币设置'),
					array('title'=>'我的站点--支付参数--虚拟币设置（开启至少一个虚拟币，这里才会显示）','type'=>'tip'),
				)
			),
			array(
				'title'=>'分销设置',
				'list'=>array(
					array('title'=>'这里只显示基础分销的设置','type'=>'tip'),
					array('type'=>'title','title'=>'门店分佣设置'),
					array('type'=>'string','name'=>'commissions.store_sale','title'=>'门店绩效设置','help'=>'如果不填写或填写0，则默认为门店统一设置（门店--门店设置--门店绩效%），如果带%则为按成交价格的比例计算；如果是数字，表示是固定金额！'),
					array('type'=>'string','name'=>'commissions.store_order_rate','title'=>'门店分佣设置','help'=>'如果不填写或填写0，则默认为门店统一设置（门店--门店设置--门店分佣%），如果带%则为按成交价格的比例计算；如果是数字，表示是固定金额！'),
					array('type'=>'string','name'=>'commissions.store_workersale','title'=>'店员分佣设置','help'=>'如果不填写或填写0，则默认为门店统一设置（门店--门店设置--店员分佣%），如果带%则为按成交价格的比例计算；如果是数字，表示是固定金额！'),
					array('type'=>'radio','title'=>'分享奖励','name'=>'commissions.fen_feetype','options'=>'xuni_types','help'=>'只有分享给新用户才能获得奖励'),
					array('type'=>'unitstring','title'=>'分享奖励','unit'=>'个','name'=>'commissions.fen_feenum'),
				)
			),
			array(
				'title'=>'分享关注',
				'list'=>array(
					array('name'=>'needfollow','title'=>'购买强制关注','type'=>'radio','options'=>'needfollow','help'=>''),
					array('name'=>'followtip','title'=>'关注提示','type'=>'string','show'=>array('needfollow'=>'1'),'help'=>''),
					array('name'=>'followurl','title'=>'关注引导链接','type'=>'link','help'=>'购买项目关注，跳转的链接，如果为空默认为系统关注页'),
					array('name'=>'share_title','title'=>'分享标题','type'=>'string','help'=>''),
					array('name'=>'share_icon','title'=>'分享图标','type'=>'image','help'=>''),
					array('name'=>'share_desc','title'=>'分享描述','type'=>'text','help'=>'如果不填写，则使用项目副标题，如项目副标题为空则使用店铺名称'),
				)
			),
			array(
				'title'=>'卖家通知',
				'list'=>array(
					array('name'=>'noticemid','title'=>'选择通知人','type'=>'choose_title','action'=>'core/member.get_member/','choose_title'=>'选择会员','help'=>'填系统会员ID'),
					array('name'=>'noticetype','title'=>'通知方式','type'=>'checkbox','options'=>'noticetype','help'=>''),					
				)
			),
			array(
				'title'=>'自定义表单',
				'list'=>array(
							array('type'=>'tip','title'=>'[温馨提示]：优先显示项目自定义的表单，其次才显示统一表单：设置--自定义表单--项目订单自定义表单！'),
							array('name'=>'diyformtype','title'=>'自定义表单类型','type'=>'radio','options'=>'diyformtype','help'=>''),
							array('name'=>'diyformid','title'=>'选择表单模板','type'=>'select','options'=>'diyformtemp','show'=>array('diyformtype'=>2),'help'=>''),
							array('type'=>'array','show'=>array('diyformtype'=>1),'name'=>'diyfields','add_tip'=>'添加一个字段（温馨提示：已经有用户提交过数据的表单，不允许再编辑和添加新字段！）',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
								//	array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'filed_type','option_title'=>'type'),
								//	array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
							),	
				)
			),
			array(
				'title'=>'扩展属性',
				'list'=>array(
					array('name'=>'labelid','title'=>'选择标签组','type'=>'select','options'=>'labels','help'=>'选择标签组后，手机端项目详情会显示相应的标签！'),
					array('name'=>'iscashier','title'=>'支持收银台','type'=>'switch'),
					//array('name'=>'cashpay','title'=>'货到付款','type'=>'switch'),
					array('name'=>'invoice','title'=>'发票','type'=>'switch'),
					array('name'=>'repair','title'=>'保修','type'=>'switch'),
				)
			),
		)
	);
	$return['xuni_types']=credit_types();
	$return['de_sales']=$this->de_sales;
	//获取分类
	$groupspram=array('is_fuwu'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$myshop['mid'],'parentid'=>0,'onepluginname'=>'jy_mfuwu');
	$return['pcates']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	$xuni_types=plugin('jy_weishop')->xuni_types;
	foreach($xuni_types as $k=>$xv){
		$htmls['edits'][7]['list'][]=array('type'=>'switch','name'=>'commissions.credit'.$k.'_diclose','title'=>$xv.'是否关闭抵扣');
		$htmls['edits'][7]['list'][]=array('type'=>'unitstring','unit'=>'%','title'=>$xv.'最大抵现比例','name'=>'commissions.credit'.$k.'_dirate','help'=>'默认100%抵扣，如果填写50，表示最大抵扣订单额度的50%');
		$htmls['edits'][7]['list'][]=array('type'=>'unitstring','unit'=>'个','title'=>'返'.$xv,'name'=>'commissions.credit'.$k.'_fan','help'=>'默认不返，填写具体数字后，返多少'.$xv);
	}
	if($this->plugin_exists('jy_commission')){
		$jy_commission=model('jy_commission/model')->getgoodshtml($return['item']['id']);
		if($jy_commission && $jy_commission['item']){
			$return['jy_commissionlevels']=$jy_commission['levels'];
			$return['item']=array_merge($return['item'],$jy_commission['item']);
			foreach($jy_commission['htmls'] as $jv){
				$htmls['edits'][8]['list'][]=$jv;
			}
		}
	}
	$dispatchtype=array('0'=>'运费模板','1'=>'统一运费','2'=>'默认模板');
	$totalcnf=array('0'=>'拍下减库存','1'=>'付款减库存','2'=>'永不减库存');
	$needfollow=array('0'=>'否','1'=>'是');
	$noticetype=array('1'=>'付款通知','2'=>'买家收货通知');
	$verifytype=array('0'=>'按订单核销','1'=>'按次核销','2'=>'按消费码核销');
	$return['verifygoodslimittype']=array('0'=>'购买后有效','1'=>'指定过期日期');
	$diyformtype=array('0'=>'关闭','1'=>'自定义','2'=>'使用模板');
	$presellsendtimetype=array('0'=>'固定时间','1'=>'购买时间');
	$return['diyformtemp']=pdo_getall('jy_weishop_diyform_temp',array('uniacid'=>$_SESSION['uniacid']));
	//$return['storeids']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['labels']=pdo_getall('jy_weishop_goods_label',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$levels=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels2']=$levels;
	$return['levels']=array();
	$return['levels'][0]='默认会员';
	foreach($levels as $lv){
		$return['levels'][$lv['id']]=$lv['title'];
	}
	$groups=pdo_getall('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=array();
	foreach($groups as $lv){
		$return['groups'][$lv['id']]=$lv['title'];
	}
	$return['dispatchid']=pdo_getall('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletype']=$this->saletypes;
	$return['dispatchtype']=$dispatchtype;
	$return['totalcnf']=$totalcnf;
	$return['status']=$status;
	$return['needfollow']=$needfollow;
	$return['noticetype']=$noticetype;
	$return['verifytype']=$verifytype;
	$return['diyformtype']=$diyformtype;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/good_post');
	}else{
		$return['htmls']=$htmls;
	}
	$return['virtual_templates']=pdo_getall('jy_weishop_virtual_temp',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));
	$return['filed_type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['isbuyxiu']){
		$install = check_permission('jy_vote');
		if(empty($install)){
			exi('没有安装《场景投票》应用，不能开启‘买家秀’！','error');
		}
	}
	if(!$postdata['title']){
		exi('必须设置项目名称','error');
	}
	if(!$postdata['image']){
		exi('必须设置项目图片','error');
	}
	if(!$postdata['price']){
		//exi('必须设置项目价格','error');
	}
	if(!$postdata['content']){
		//exi('必须设置项目详情','error');
	}
	if(!$shopset['goodscodenum'] && $postdata['offcode']){
		//exi('还未设置称码长度，不能设置项目称码','error');
	}
	if($shopset['goodscodenum']!=mb_strlen($postdata['offcode'])){
		//exi('项目称码必须设置'.$shopset['goodscodenum'].'位数','error');
	}
	//计算卡密
	if($postdata['type']==2){
		$count=pdo_count('jy_weishop_virtual_data',array('uniacid'=>$_SESSION['uniacid'],'tempid'=>intval($postdata['virtual_template_id']),'mid'=>0),'id');
		if($count<intval($postdata['total'])){
			exi('你设置的库存不能大于：选择的卡密的库存数据！','error');
		}
	}
	$postdata['deductset']['deduct_bili']=intval($postdata['deductset']['deduct_bili']);
	$postdata['deductset']['deduct_maxfeebili']=$postdata['deductset']['deduct_maxfeebili'];
	$param=array(
		0=>$postdata['param0'],
		1=>$postdata['param1'],
		2=>$postdata['param2'],
		3=>$postdata['param3'],
		'param1title'=>$postdata['param1title'],
		'param2title'=>$postdata['param3title'],
		'param3title'=>$postdata['param3title'],
	);
	$param=ijson_encode($param);
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'video'=>trim($postdata['video']),
		'subtitle'=>trim($postdata['subtitle']),
		'shorttitle'=>trim($postdata['shorttitle']),
		'keywords'=>trim($postdata['keywords']),
		'saletype'=>trim($postdata['saletype']),
		'type'=>intval($postdata['type']),
		'paixu'=>intval($postdata['paixu']),
		'price'=>floatval($postdata['price']),
		'originalprice'=>floatval($postdata['originalprice']),
		'marketprice'=>floatval($postdata['marketprice']),
		'costprice'=>floatval($postdata['costprice']),
		'images'=>ijson_encode($postdata['images']),
		'quality'=>intval($postdata['quality']),
		'salenum'=>intval($postdata['salenum']),
		'unit'=>$postdata['unit']?trim($postdata['unit']):'次',
		'isshowsales'=>intval($postdata['isshowsales']),
		/*'fullnum'=>intval($postdata['fullnum']),
		'fullmoney'=>intval($postdata['fullmoney']),
		'fullnoareas'=>trim($postdata['fullnoareas']),
		'dispatchtype'=>intval($postdata['dispatchtype']),
		'weight'=>round($postdata['weight'],2),
		'dispatchid'=>intval($postdata['dispatchid']),
		'dispatchprice'=>intval($postdata['dispatchprice']),*/
		'status'=>intval($postdata['status']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'iscashier'=>intval($postdata['iscashier']),
		'invoice'=>intval($postdata['invoice']),
		'cashpay'=>intval($postdata['cashpay']),
		'repair'=>intval($postdata['repair']),
		'seven'=>intval($postdata['seven']),
		'virtualsendcont'=>trim($postdata['virtualsendcont']),
		'virtual_template_id'=>intval($postdata['virtual_template_id']),
		
		'virtualsend'=>intval($postdata['virtualsend']),
		//库存
		'goodssn'=>trim($postdata['goodssn']),
		'productsn'=>trim($postdata['productsn']),
		
		'isshowtotal'=>intval($postdata['isshowtotal']),
		'total'=>intval($postdata['total']),
		'totalcnf'=>intval($postdata['totalcnf']),
		
		//参数
		'param'=>$param,
		//详情
		'content'=>trim($postdata['content']),
		'buyshow'=>intval($postdata['buyshow']),
		'buycontent'=>trim($postdata['buycontent']),
		//购买权限
		'no_de_sales'=>trim($postdata['no_de_sales']),
		'maxbuy'=>intval($postdata['maxbuy']),
		'minbuynum'=>intval($postdata['minbuynum']),
		'usermaxbuy'=>intval($postdata['usermaxbuy']),
		'showlevels'=>trim($postdata['showlevels']),
		'buylevels'=>trim($postdata['buylevels']),
		'showgroups'=>trim($postdata['showgroups']),
		'buygroups'=>trim($postdata['buygroups']),
		//DIY价格
		'isdiyprice'=>intval($postdata['isdiyprice']),
		'diyprice'=>ijson_encode($postdata['diyprice']),
		//分享关注
		'needfollow'=>intval($postdata['needfollow']),
		'followtip'=>trim($postdata['followtip']),
		'followurl'=>trim($postdata['followurl']),
		'share_title'=>trim($postdata['share_title']),
		'share_icon'=>trim($postdata['share_icon']),
		'share_desc'=>trim($postdata['share_desc']),
		
		//卖家通知
		'noticemid'=>intval($postdata['noticemid']),
		'noticetype'=>trim($postdata['noticetype']),
		
		//核销
		'isverify'=>intval($postdata['isverify']),
		'verifytype'=>intval($postdata['verifytype']),
		'usetime'=>intval($postdata['usetime']),
		'verifygoodsnum'=>intval($postdata['verifygoodsnum']),
		'verifygoodsdays'=>intval($postdata['verifygoodsdays']),
		'verifygoodslimittype'=>intval($postdata['verifygoodslimittype']),
		'verifygoodslimitdate'=>intval(strtotime($postdata['verifygoodslimitdate'])),
		//店铺信息
		'storeids'=>ijson_encode($postdata['storeids']),
		//营销设置
		'isdiscount'=>intval($postdata['isdiscount']),
		'deductset'=>ijson_encode($postdata['deductset']),
		'fang_credit2'=>trim($postdata['fang_credit2']),
		//分销设置
		'commissions'=>ijson_encode($postdata['commissions']),
		//自定义表单
		'diyfields'=>$postdata['diyfields']?ijson_encode($postdata['diyfields']):'',
		'diyformid'=>intval($postdata['diyformid']),
		'diyformtype'=>intval($postdata['diyformtype']),
		//扩展属性
		'labelid'=>intval($postdata['labelid']),
		'isoffcode'=>intval($postdata['isoffcode']),
		'offcode'=>trim($postdata['offcode']),
		'offcodeunit'=>trim($postdata['offcodeunit']),		
		'groupprice'=>trim($postdata['groupprice']),
		'groupnum'=>trim($postdata['groupnum']),
		'grouptime'=>trim($postdata['grouptime']),
		'no_onebuy'=>intval($postdata['no_onebuy']),
		'pcate'=>intval($postdata['pcate']),
		'ccate'=>intval($postdata['ccate']),
		'scate'=>intval($postdata['scate']),
		'api_data'=>ijson_encode($postdata['api_data']),
		'actionurl'=>'jy_mfuwu/app.goods.detail/',
	);
	//如果项目类型为计时/计次项目，默认开启核销
	if($data['type']=='3'){
		$data['isverify']=1;
	}
	//供货商
	if($postdata['supplier_id']){
		$data['api_id']=$postdata['supplier_id'];
		$data['api_pluginname']='jy_supplier';
	}
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['onepluginname']='jy_mfuwu';
		$data['type']=4;
		$data['merchid']=$merchid;
		$jy_merch=plugin('jy_merch')->plugin_setting('setting');
		if($jy_merch['isgoodsshenhe']){
				$data['nochecked']=1;
		}else{
				$data['nochecked']=0;
		}
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	//实体项目快递配送相关字段入库
	if($postdata['type']==0){
		$dispatch=array(
			'fullnum'=>intval($postdata['dispatch']['fullnum']),
			'fullmoney'=>intval($postdata['dispatch']['fullmoney']),
			'fullnoareas'=>trim($postdata['dispatch']['fullnoareas']),
			'type'=>intval($postdata['dispatch']['type']),
			'template_id'=>intval($postdata['dispatch']['template_id']),
			'weight'=>round($postdata['dispatch']['weight'],2),
			'price'=>round($postdata['dispatch']['price'],2),
		);
		$check=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$goodsid),'goodsid');
		if($check){
			pdo_update('jy_weishop_goods_dispatch',$dispatch,array('goodsid'=>$goodsid));
		}else{
			$dispatch['goodsid']=$goodsid;
			$dispatch['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_goods_dispatch',$dispatch);
		}
	}
	
	
	//整理项目规格数据
	if($postdata['specs']){
		$spec_ids=array();//接收当前生效的规格ID
		$spec_option_ids=array();//接收当前生效的规格项ID
		$spec_goods_ids=array();//接收当前生效的规格项目ID
		//第一步，整理规格和规格项
		foreach($postdata['specs']['specs'] as $spec_key=>&$spec){
			if(!intval($spec['id'])){				
				//数据库里面没有该规格
				$new_spec=array(
					'uniacid'=>$_SESSION['uniacid'],
					'goodsid'=>$goodsid,
					'paixu'=>$spec_key,
					'title'=>$spec['title'],
				);
				pdo_insert('jy_weishop_goods_spec',$new_spec);
				$spec['oldid']=$spec['id'];
				$spec['id']=pdo_insertid();
			}else{
				pdo_update('jy_weishop_goods_spec',array('title'=>$spec['title']),array('id'=>$spec['id']));
			}
			$spec_ids[]=$spec['id'];
			foreach($spec['options'] as $option_key=>&$option){
				if(!intval($option['id'])){
					$new_spec_option=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'specid'=>$spec['id'],
						'title'=>$option['title'],
						'paixu'=>$option_key,
					);
					pdo_insert('jy_weishop_goods_spec_item',$new_spec_option);
					$option['oldid']=$option['id'];
					$option['id']=pdo_insertid();
				}else{
					pdo_update('jy_weishop_goods_spec_item',array('title'=>$option['title']),array('id'=>$option['id']));
				}
				$spec_option_ids[]=$option['id'];
			}
		}
		
		
		//第二步，整理规格项目数据
		foreach($postdata['specs']['tds'] as $key=>$spec_goods){	
			$data=array(
				'paixu'=>$key,
				'thumb'=>trim($spec_goods['image']),
				'stock'=>intval($spec_goods['num']),
				'price'=>$spec_goods['price'],
				'productprice'=>$spec_goods['old_price'],
				'costprice'=>$spec_goods['cost_price'],
				'ya_price'=>$spec_goods['ya_price'],
				'goodssn'=>trim($spec_goods['bm']),
				'productsn'=>trim($spec_goods['tm']),
				'weight'=>round($spec_goods['weight'],2),
				'zu_data'=>ijson_encode($spec_goods['zu_data']),
			);
			if($spec_goods['id']){
				pdo_update('jy_weishop_goods_option',$data,array('id'=>intval($spec_goods['id']),'uniacid'=>$_SESSION['uniacid']));
			}else{
				//新的规格项目，根据规格值找到相应规格ID
				$data['specs']=array();
				foreach($postdata['specs']['specs'] as $sp){
					$sp_id=$sp['oldid']?:$sp['id'];
					foreach($sp['options'] as $op){
						$op_id=$op['oldid']?:$op['id'];
						if($spec_goods[$sp_id] && $spec_goods[$sp_id]['id']==$op_id){
							$data['specs'][]=$op['id'];
							break;
						}
					}
				}
				$data['specs']=implode('_',$data['specs']);
				$data['uniacid']=$_SESSION['uniacid'];
				$data['goodsid']=$goodsid;
				pdo_insert('jy_weishop_goods_option',$data);
				$spec_goods['id']=pdo_insertid();
			}
			$spec_goods_ids[]=$spec_goods['id'];
		}
		//清理数据库中弃用的规格、规格项、规格项目
		if(!empty($spec_ids)){
			pdo_delete('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_ids));
		}else{
			pdo_delete('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid));
		}
		if(!empty($spec_option_ids)){
			pdo_delete('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_option_ids));
		}else{
			pdo_delete('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid));
		}
		if(!empty($spec_goods_ids)){
			pdo_delete('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_goods_ids));
		}else{
			pdo_delete('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid));
		}
		$options = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price asc');
		$options2 = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price desc');
		if($options){
			$upgoods =array();
			$upgoods['minprice'] = $options[0]['price'];
			$upgoods['maxprice'] = $options2[0]['price'];
			$upgoods['total'] = pdo_getsum('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'stock');
			pdo_update('jy_weishop_goods',$upgoods,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	if($this->plugin_exists('jy_commission')){
		$post=array(
			'id'=>intval($postdata['jy_commission_id']),
			'goods_id'=>$goodsid,
			'ignore'=>intval($postdata['jy_commission_ignore']),
			'commissions'=>$postdata['jy_commission'],
		);
		model('jy_commission/model')->upgoods($post);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>0),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>1),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
if($_RQ['op']=='setstatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>2),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('售罄成功！');
}
if($_RQ['op']=='setstatus3'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('回收成功！');
}
if($_RQ['op']=='upseven'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('seven'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('设置成功！');
}
if($_RQ['op']=='uplegao'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择项目','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('onepluginname'=>''),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('回收成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upgoods'){
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','showlevels','buylevels','showgroups','buygroups'));
	foreach($goods as &$item){
		if($item['showlevels']){
			$fastvalue=substr($item['showlevels'],0,1);
			if($fastvalue!=';'){
				$item['showlevels']=';'.$item['showlevels'];
			}
			$lastvalue=substr($item['showlevels'],-1,1);
			if($lastvalue!=';'){
				$item['showlevels']=$item['showlevels'].';';
			}
		}else{
			$item['showlevels']='';
		}
		if($item['buylevels']){
			$fastvalue=substr($item['buylevels'],0,1);
			if($fastvalue!=';'){
				$item['buylevels']=';'.$item['buylevels'];
			}
			$lastvalue=substr($item['buylevels'],-1,1);
			if($lastvalue!=';'){
				$item['buylevels']=$item['buylevels'].';';
			}
		}else{
			$item['buylevels']='';
		}
		if($item['showgroups']){
			$fastvalue=substr($item['showgroups'],0,1);
			if($fastvalue!=';'){
				$item['showgroups']=';'.$item['showgroups'];
			}
			$lastvalue=substr($item['showgroups'],-1,1);
			if($lastvalue!=';'){
				$item['showgroups']=$item['showgroups'].';';
			}
		}else{
			$item['showgroups']='';
		}
		if($item['buygroups']){
			$fastvalue=substr($item['buygroups'],0,1);
			if($fastvalue!=';'){
				$item['buygroups']=';'.$item['buygroups'];
			}
			$lastvalue=substr($item['buygroups'],-1,1);
			if($lastvalue!=';'){
				$item['buygroups']=$item['buygroups'].';';
			}
		}else{
			$item['buygroups']='';
		}
		pdo_update('jy_weishop_goods',array('showlevels'=>trim($item['showlevels']),'buylevels'=>trim($item['buylevels']),'showgroups'=>trim($item['showgroups']),'buygroups'=>trim($item['buygroups'])),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('一键更新项目成功！');
}
if($_RQ['op']=='uponepluginname'){
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','showlevels','buylevels','showgroups','buygroups'));
	foreach($goods as &$item){
		
	}
	exi('一键更新项目成功！');
}
if($_RQ['op']=='copy'){
	$id=intval($_RQ['id']);
	$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	unset($goods['id']);
	pdo_insert('jy_weishop_goods',$goods);
	$newgoodsid=pdo_insertid();
	$option=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(count($option)){
		foreach($option as $ov){
			unset($ov['id']);
			$ov['goodsid']=$newgoodsid;
			pdo_insert('jy_weishop_goods_option',$ov);
		}
	}
	$spec=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(count($spec)){
		foreach($spec as $ov){
			unset($ov['id']);
			$ov['goodsid']=$newgoodsid;
			pdo_insert('jy_weishop_goods_spec',$ov);
		}
	}
	$spec_item=pdo_getall('jy_weishop_goods_spec_item',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(count($spec_item)){
		foreach($spec_item as $ov){
			unset($ov['id']);
			$ov['goodsid']=$newgoodsid;
			pdo_insert('jy_weishop_goods_spec_item',$ov);
		}
	}	
	exi('复制项目成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择项目','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}